﻿using Newtonsoft.Json.Serialization;
using RockBus.Logging.Interfaces;
using System.Web.Http;

namespace RockBus.Services.WebApi
{
    public class WebApiConfiguration
    {
        public WebApiConfiguration(ILogger logger)
        {
            this.Logger = logger;
        }

        private ILogger Logger { get; set; }

        public void Configure(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();

            // Configure Http Routes
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            // Configure Json camelcase serialization
            config.Formatters.Remove(config.Formatters.XmlFormatter);
            var jsonFormatter = config.Formatters.JsonFormatter;
            jsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
            jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

            // Configure DependencyResolver
            config.DependencyResolver = new WebApiDependencyResolver();
        }
    }
}
